//////////////////////////////////////////////////////////////////////
/// File : script_helper
/// \brief Scripting helper functions
/// 
/// Functions to simplify the process of using LuaPlus as a 
/// configuration language. Examples include functions for quickly
/// getting a string/float/value from LUA. Functions for redirecting
/// output (to console ?). etc.
//////////////////////////////////////////////////////////////////////

#ifndef SCRIPTHELPER_H_
#define SCRIPTHELPER_H_

#include <string>
#include "math/vec4.h"
#include "math/vec3.h"
#include "math/vec2.h"

#include "luaplus/LuaPlus.h"
namespace vfs
{
	struct LoadedBlock;
}


namespace script 
{

class ScriptHelper
{
public:
/// Functions to extract a value from an object, with a default
    static bool GetBool( LuaPlus::LuaObject&  _Obj, bool _Default = false );
    static int GetInt ( LuaPlus::LuaObject&  _Obj, int _Default = 0 );
	static float GetFloat ( LuaPlus::LuaObject&  _Obj, float _Default = 0.0f );
    static std::string GetString ( LuaPlus::LuaObject&  _Obj, 
                                   const std::string & _Default = "" );

    static math::Vec2 GetVec2(LuaPlus::LuaObject& _Obj, const math::Vec2& _Default = math::Vec2());
    static math::Vec3 GetVec3(LuaPlus::LuaObject& _Obj, const math::Vec3& _Default = math::Vec3());
    static math::Vec4 GetVec4(LuaPlus::LuaObject& _Obj, const math::Vec4& _Default = math::Vec4());

/// functions to extract a value directly from the State, assuming a name.
	static int GetInt (	LuaPlus::LuaState & _State, 
                        const std::string & _Table, 
						const std::string & _Name, 
                        int _Default = 0 );
	static bool GetBool(LuaPlus::LuaState & _State, 
                        const std::string & _Table, 
						const std::string & _Name, 
                        bool _Default = false );


	static float GetFloat ( LuaPlus::LuaState & _State, 
                            const std::string & _Table,
			                const std::string & _Name, 
                            float _Default = 0.0f );

	static std::string GetString ( LuaPlus::LuaState & _State, 
  								   const std::string & _Table, 
								   const std::string & _Name, 
								   const std::string & _Default = "" );

    static math::Vec2   GetVec2     (	LuaPlus::LuaObject& _State, 
                                        const std::string & _Table,
										const std::string & _Name, 
                                        const math::Vec2& _Default = math::Vec2());
    
    static math::Vec3   GetVec3      (	LuaPlus::LuaObject& _State, 
                                        const std::string & _Table,
                                        const std::string & _Name, 
                                        const math::Vec3& _Default = math::Vec3());

	static math::Vec4   GetVec4      (	LuaPlus::LuaObject& _State, 
                                        const std::string & _Table,
                                        const std::string & _Name, 
                                        const math::Vec4& _Default = math::Vec4());

    // functions to extract a value directly from the Table, assuming a name.
	static int GetTableInt ( LuaPlus::LuaObject& _State, 
							 const std::string & _Name, 
							 int _Default = 0 );

	static bool GetTableBool(LuaPlus::LuaObject& _State, 
							 const std::string & _Name, 
                             bool _Default = false );


	static float GetTableFloat ( LuaPlus::LuaObject& _State, 
								 const std::string & _Name, 
								 float _Default = 0.0f );

	static std::string GetTableString (	LuaPlus::LuaObject& _State, 
										const std::string & _Name, 
										const std::string & _Default = "" );

    static math::Vec2   GetTableVec2 (	LuaPlus::LuaObject& _State, 
										const std::string & _Name, 
                                        const math::Vec2& _Default = math::Vec2());
    
    static math::Vec3   GetTableVec3 (	LuaPlus::LuaObject& _State, 
										const std::string & _Name, 
                                        const math::Vec3& _Default = math::Vec3());

    static math::Vec4   GetTableVec4 (	LuaPlus::LuaObject& _State, 
										const std::string & _Name, 
                                        const math::Vec4& _Default = math::Vec4());


    // Get an object from a state or another object. Returns NULL if not found.
	static LuaPlus::LuaObject GetObj ( LuaPlus::LuaState & _State, 
                                       const std::string& _Name );

	static LuaPlus::LuaObject GetObj ( LuaPlus::LuaObject& _Object, 
                                       const std::string& _Name );

    static bool RegisterLibrary ( LuaPlus::LuaState & _State, 
                                  const char * _szName, 
                                  const luaL_reg _lib[] );

    static bool DoFile ( LuaPlus::LuaState & _State, 
                         const std::string & _Name );
    static bool DoBlock ( LuaPlus::LuaState & _State, 
							vfs::LoadedBlock& Block );

    static bool CreateTable ( LuaPlus::LuaState & _State, 
                              const std::string & _Name );

    static LuaPlus::LuaState * CreateLuaState ( );
};

} // ending namespace script

#endif // SCRIPT_HELPER_H_
